#include <chat.h>
#include <UI/ui.h>
#include <UI/ui_myFunc.h>

#include <algorithm>

using namespace std;
using namespace Chat;



void* ClientRecvMsg(void* sockfd)
{
    pthread_detach(pthread_self());
    int connfd = *(int*)sockfd;
    int nRead;

    char    buf[MAX_LINE]{}, str[MAX_LINE]{};
    Message message{};

    time_t timep;

    printf("^_^ 接收聊天信息中～\n");
    while (1) {
        /*接收服务器发来的消息*/
        nRead = recv(connfd, buf, sizeof(message), 0);
        /*recv函数返回值 <0 出错  =0 链接关闭  >0接收到的字节数*/
        if (nRead <= 0) {
            printf("您已经异常掉线，请重新登录！\n");
            close(connfd);
            *(int*)sockfd = -1;
            pthread_exit(NULL);
        }   // if

        memset(&message, 0, sizeof(message));
        memcpy(&message, buf, sizeof(buf));

        switch (message.msgType) {
        case MessageType::PERSONAL_CHAT: {
            auto iter = find_if(ChatterList.begin(),
                                ChatterList.end(),
                                [&message](pair<string, lv_obj_t* [2]> iter)
                                { return (iter.first.find(message.sendName) != string::npos); });
            if (iter == ChatterList.end()) {
                if (strcmp(message.sendName, loginUser.userName) == 0) {
                    break;
                }
                printf("发现新用户:%s,已添加进列表\n", message.sendName);
                pair<string, lv_obj_t* [2]> temp{};
                temp.first = message.sendName;
                FriendList_add_obj(string{message.sendName});
                ChatterCont_add_win(temp);
                ChatterList.push_back(temp);

                iter = (ChatterList.end() - 1);
            }
            sprintf(str,
                    "%s \t %s \t对你说： %s\n",
                    message.sendName,
                    message.msgTime,
                    message.content);
            printf("\n%s\n", str);
            ChatterCont_add_RecMsg(*iter, message);

            break;
        }

        case MessageType::GROUP_CHAT: {
            sprintf(str,
                    "%s \t %s \t发送群消息： %s\n",
                    message.sendName,
                    message.msgTime,
                    message.content);
            printf("\n%s\n", str);

            auto iter = find_if(ChatterList.begin(),
                                ChatterList.end(),
                                [&message](pair<string, lv_obj_t* [2]> iter)
                                { return (iter.first.find(message.sendName) != string::npos); });
            if (iter == ChatterList.cend()) {
                if (strcmp(message.sendName, loginUser.userName) == 0) {
                    break;
                }
                printf("发现新用户:%s,已添加进列表\n", message.sendName);
                pair<string, lv_obj_t* [2]> temp{};
                temp.first = message.sendName;
                ChatterList.push_back(temp);
                FriendList_add_obj(string{message.sendName});
            }
            GroupMsgCont_add_RecMsg(message);
        } break;

        case MessageType::RESULT:
            printf("你的操作结果：%s\n", message.content);
        default:
            break;
        }
    }
}
